package com.atguigu.dao.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User queryUserByUsername(String username) {
        String sql = "select `id`,`username`,`password`,`email` from t_user where username = ?";
        return queryForOne(User.class, sql, username);
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password) {
        String sql = "select `id`,`username`,`password`,`email` from t_user where username = ? and password = ?";
        return queryForOne(User.class, sql, username,password);
    }

    @Override
    public int saveUser(User user) {
        String sql = "insert into t_user(`username`,`password`,`email`) values(?,?,?)";
        return update(sql, user.getUsername(),user.getPassword(),user.getEmail());
    }


    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from t_user";

        Number cnt = (Number) queryForSingleValue(sql);

        return cnt.intValue();
    }

    @Override
    public List<User> queryForPageItems(int begin, Integer pageSize) {
        String sql = "select `id`,`username`,`password`,`email` from t_user limit ?,?";
        return queryForList(User.class,sql,begin,pageSize);
    }

    @Override
    public int deleteUserById(int id) {
        String sql = "DELETE FROM t_user WHERE id=?";
        return update(sql,id);
    }

    @Override
    public User queryUserById(int id) {
        String sql = "select `id`,`username`,`password`,`email` from t_user " +
                "where id=?";
        return queryForOne(User.class,sql,id);
    }

    @Override
    public void update(User user) {
        String sql = "update t_user set `username`=?,`password`=?,`email`=? where id=?";
        update(sql,user.getUsername(),user.getPassword(),user.getEmail(),user.getId());
    }

    @Override
    public User findPwdByUsername(String username) {
        String sql = "select `id`,`username`,`password`,`email` from t_user " +
                "where username=?";
        return queryForOne(User.class,sql,username);
    }
}
